home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Graphics / FlashMandel / Sources / Modules / Iff.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-20  |  9.5 KB  |  360 lines

  1. /******************************************************************************
  2. **
  3. **  Coded by Dino Papararo 23-Apr-2001
  4. **
  5. **  Based on NewIff package relased by Commodore
  6. **
  7. **  FUNCTION
  8. **
  9. **    QueryMandPic -- Examine an IFF picture.
  10. **
  11. **  SYNOPSIS
  12. **
  13. **    LONG QueryMandPic (struct ILBMInfo *,struct MandelChunk *,UBYTE *);
  14. **
  15. **  DESCRIPTION
  16. **
  17. **    Passed an initialized ILBMInfo with a not-in-use IFFHandle, a MandelChunk
  18. **
  19. **    structure and a filename, will open an ILBM, fill in ilbm->camg and
  20. **
  21. **    ilbm->bmhd, and close the ILBM.
  22. **
  23. **    This allows you to determine if the ILBM is a size and
  24. **
  25. **    type you want to deal with.
  26. **
  27. **    For succes is necessary the ID_MAND into the iff file !
  28. **
  29. **    Returns 0 for success or an IFFERR (libraries/iffparse.h).
  30. **
  31. **
  32. **  FUNCTION
  33. **
  34. **    LoadMandPic -- Load an IFF picture.
  35. **
  36. **  SYNOPSIS
  37. **
  38. **    LONG LoadMandPic (struct ILBMInfo *,UBYTE *);
  39. **
  40. **  DESCRIPTION
  41. **
  42. **    Function uses a ILBMInfo struct with record ParseInfo.iff initialized
  43. **
  44. **    with AllocIFF() function, and a pointer to a FileName.
  45. **
  46. **    It's necessary a valid Window RPort and ViewPort initialized in the passed
  47. **
  48. **    ILBM Structure for the BODY and COLORS, at end all memory will be freed.
  49. **
  50. **
  51. **  FUNCTION
  52. **
  53. **    SaveMandPic -- save a screen as IFF picture.
  54. **
  55. **  SYNOPSIS
  56. **
  57. **    LONG SaveMandPic (struct ILBMInfo *,struct Chunk *,struct Chunk *,UBYTE *);
  58. **
  59. **  DESCRIPTION
  60. **
  61. **    Function uses a ILBMInfo struct with record ParseInfo.iff initialized
  62. **
  63. **    with AllocIFF() function, two custom chunks, and a pointer to a FileName.
  64. **
  65. **    In the 1st custom chunk I put the copyright infos and in the 2nd the
  66. **
  67. **    special chunk MAND used for window limits, fractal limits, iterations
  68. **
  69. **    type...
  70. **
  71. **    Function will save the icon file too with support for NewIcons.
  72. **
  73. ******************************************************************************/
  74. #include <exec/types.h>
  75. #include <intuition/intuitionbase.h>
  76. #include <intuition/screens.h>
  77. #include <graphics/gfxbase.h>
  78.  
  79. #include <iffp/ilbmapp.h>
  80. #include <iffp/ilbm.h>
  81. #include <iffp/packer.h>
  82.  
  83. #include <proto/exec.h>
  84. #include <proto/intuition.h>
  85. #include <proto/graphics.h>
  86. #include <proto/iffparse.h>
  87.  
  88. #include "flashmandel.h"
  89.  
  90. #define NOMAND 115L
  91. #define MANDERR 116L
  92.  
  93. #define FROMBLACK 0
  94.  
  95. /* local function prototypes */
  96.  
  97. IMPORT BOOL Fade (struct Window *,ULONG *,ULONG,ULONG,BOOL);
  98.  
  99. LONG QueryMandPic (struct ILBMInfo *,struct MandelChunk *,UBYTE *filename);
  100.  
  101. LONG LoadMandPic (struct ILBMInfo *,UBYTE *);
  102.  
  103. LONG SaveMandPic (struct ILBMInfo *,struct Chunk *,struct Chunk *,UBYTE *);
  104.  
  105. LONG SavePalette (struct ILBMInfo *,struct Chunk *,UBYTE *);
  106.  
  107. LONG LoadPalette (struct ILBMInfo *,UBYTE *);
  108.  
  109. /*----------------------------------------------------------------------*/
  110.  
  111. /* queryilbm
  112.  *
  113.  * Passed an initilized ILBMInfo with a not-in-use IFFHandle,
  114.  *   and a filename,
  115.  *   will open an ILBM, fill in ilbm->camg and ilbm->bmhd,
  116.  *   and close the ILBM.
  117.  *
  118.  * This allows you to determine if the ILBM is a size and
  119.  *   type you want to deal with.
  120.  *
  121.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  122.  */
  123.  
  124. LONG QueryMandPic (struct ILBMInfo *ilbm,struct MandelChunk *ManChk,UBYTE *filename)
  125. {
  126. LONG error;
  127.  
  128. BitMapHeader *bmhd;
  129.  
  130. struct MandelChunk *TmpManChk;
  131.  
  132.   if (! (ilbm->ParseInfo.iff)) return (CLIENT_ERROR);
  133.  
  134.   error = openifile (&(ilbm->ParseInfo),filename,IFFF_READ);
  135.  
  136.   if (! error)
  137.   {
  138.      error = parseifile (&(ilbm->ParseInfo),ID_FORM,ID_ILBM,ilbm->ParseInfo.propchks,ilbm->ParseInfo.collectchks,ilbm->ParseInfo.stopchks);
  139.  
  140.      if ((error == NULL) || (error == IFFERR_EOC) || (error == IFFERR_EOF))
  141.      {
  142.         if (contextis (ilbm->ParseInfo.iff,ID_ILBM,ID_FORM))
  143.         {
  144.            if (TmpManChk = (struct MandelChunk *) findpropdata (ilbm->ParseInfo.iff,ID_ILBM,ID_MAND))
  145.            {           
  146.               CopyMem (TmpManChk,ManChk,sizeof (struct MandelChunk));
  147.               
  148.               if (bmhd = (BitMapHeader *) findpropdata (ilbm->ParseInfo.iff,ID_ILBM,ID_BMHD))
  149.               {
  150.                  CopyMem (bmhd,&ilbm->Bmhd,sizeof (BitMapHeader));
  151.  
  152.                  ilbm->camg = getcamg (ilbm);
  153.               }
  154.                           
  155.               else error = NOFILE;             
  156.             }
  157.  
  158.            else error = NOMAND;
  159.         }
  160.  
  161.         else error = NOFILE;
  162.      }
  163.  
  164.      closeifile (&(ilbm->ParseInfo));
  165.   }
  166.  
  167.   return (error);
  168. }
  169.  
  170. /* LoadMandPic
  171.  *
  172.  * Passed a not-in-use IFFHandle, an initialized ILBMInfo, and filename,
  173.  *   will load an ILBM into your already opened ilbm->scr, setting up
  174.  *   ilbm->Bmhd, ilbm->camg, ilbm->colortable, and ilbm->ncolors
  175.  *   and loading the colors into the screen's viewport
  176.  *
  177.  *   Note that ncolors may be more colors than you can LoadRGB4.
  178.  *   Use MIN(ilbm->ncolors,vp->ColorMap->Count) for color count if
  179.  *   you change the colors yourself using 1.3/2.0 functions.
  180.  *
  181.  * V39 - unless ilbm->IFFPFlags & IFFPF_NOCOLOR32, will do 32-bit
  182.  *   color load under V39 and higher
  183.  *
  184.  * Returns 0 for success or an IFFERR (libraries/iffparse.h)
  185.  *
  186.  * NOTE - LoadMandPic () keeps the IFFHandle open so you can copy
  187.  *   or examine other chunks.  You must call closeifile(iff,ilbm)
  188.  *   to close the file and deallocate the parsed context
  189.  *
  190.  */
  191.  
  192. LONG LoadMandPic (struct ILBMInfo *ilbm,UBYTE *filename)
  193. {
  194. struct BitMap *TmpBM;
  195.  
  196. LONG error;
  197.  
  198.   if (! (ilbm->ParseInfo.iff)) return (CLIENT_ERROR);
  199.  
  200.   if (! ilbm->scr) return (CLIENT_ERROR);
  201.  
  202.   if (! (ilbm->vp)) ilbm->vp = &ilbm->scr->ViewPort;
  203.  
  204.   error = openifile (&ilbm->ParseInfo,filename,IFFF_READ);
  205.  
  206.   if (! error)
  207.   {
  208.      error = parseifile (&ilbm->ParseInfo,ID_FORM,ID_ILBM,ilbm->ParseInfo.propchks,ilbm->ParseInfo.collectchks,ilbm->ParseInfo.stopchks);
  209.  
  210.      if ((! error) || (error == IFFERR_EOC) || (error == IFFERR_EOF))
  211.      {
  212.         if (contextis (ilbm->ParseInfo.iff,ID_ILBM,ID_FORM))
  213.         {
  214.            if (TmpBM = AllocBitMap ((ULONG) ilbm->win->Width,(ULONG) ilbm->win->Height,(ULONG) ilbm->wrp->BitMap->Depth,BMF_INTERLEAVED | BMF_CLEAR | BMF_MINPLANES,NULL))
  215.            {
  216.               if (! (error = loadbody (ilbm->ParseInfo.iff,TmpBM,&ilbm->Bmhd)))
  217.  
  218.                  BltBitMapRastPort (TmpBM,(LONG) ilbm->win->LeftEdge,(LONG) ilbm->win->TopEdge,ilbm->wrp,(LONG) ilbm->win->LeftEdge,(LONG) ilbm->win->TopEdge,(LONG) ilbm->win->Width,(LONG) ilbm->win->Height,0xC0);
  219.  
  220.               FreeBitMap (TmpBM);
  221.            }
  222.  
  223.            if (! (getcolors (ilbm)))
  224.            {
  225.  
  226.               Fade (ilbm->win,(ULONG *) (ilbm->colorrecord),25L,1L,FROMBLACK);
  227.  
  228. //              setcolors (ilbm,ilbm->vp);
  229.  
  230.               freecolors (ilbm);
  231.            }
  232.         }
  233.  
  234.         else error = NOFILE;
  235.      }
  236.  
  237.      closeifile (&(ilbm->ParseInfo));
  238.   }
  239.  
  240.   return (error);
  241. }
  242.  
  243. LONG SaveMandPic (struct ILBMInfo *ilbm,struct Chunk *chunklist1,struct Chunk *chunklist2,UBYTE *filename)
  244. {
  245. struct BitMap *TmpBM;
  246.  
  247. Color32 *colortable32;
  248.  
  249. UWORD count;
  250.  
  251. ULONG modeid;
  252.  
  253. LONG error = IFFERR_NOMEM;
  254.  
  255.   modeid = GetVPModeID (ilbm->vp);
  256.  
  257.   count = ilbm->vp->ColorMap->Count;
  258.  
  259.   if (colortable32 = (Color32 *) AllocVec ((ULONG) (sizeof (Color32) * count),MEMF_CLEAR))
  260.   {    
  261.      if (TmpBM = AllocBitMap ((ULONG) ilbm->win->Width,(ULONG) ilbm->win->Height,(ULONG) ilbm->wrp->BitMap->Depth,BMF_INTERLEAVED | BMF_CLEAR | BMF_MINPLANES,NULL))
  262.      {
  263.         GetRGB32 (ilbm->vp->ColorMap,0L,(ULONG) count,(ULONG *) colortable32);
  264.  
  265.         BltBitMap (ilbm->wrp->BitMap,(LONG) ilbm->win->LeftEdge,(LONG) ilbm->win->TopEdge,TmpBM,(LONG) ilbm->win->LeftEdge,(LONG) ilbm->win->TopEdge,(LONG) ilbm->win->Width,(LONG) ilbm->win->Height,0xC0,0xFF,NULL);
  266.  
  267.         error = saveilbm (ilbm,TmpBM,modeid,ilbm->win->Width,ilbm->win->Height,ilbm->win->Width,ilbm->win->Height,colortable32,count,32,mskNone,0,chunklist1,chunklist2,filename);
  268.  
  269.         FreeBitMap (TmpBM);
  270.      }
  271.  
  272.      FreeVec (colortable32);
  273.   }
  274.  
  275.   return (error);
  276. }
  277.  
  278. LONG LoadPalette (struct ILBMInfo *ilbm,UBYTE *filename)
  279. {
  280. LONG error;
  281.  
  282.   error = openifile (&(ilbm->ParseInfo),filename,IFFF_READ);
  283.  
  284.   if (! error)
  285.   {
  286.      error = parseifile (&(ilbm->ParseInfo),ID_FORM,ID_ILBM,ilbm->ParseInfo.propchks,ilbm->ParseInfo.collectchks,ilbm->ParseInfo.stopchks);
  287.  
  288.      if ((! error) || (error == IFFERR_EOC) || (error == IFFERR_EOF))
  289.      {
  290.         if (! (error = getcolors (ilbm)))
  291.         {
  292.  
  293.              Fade (ilbm->win,(ULONG *) (ilbm->colorrecord),25L,1L,FROMBLACK);
  294.  
  295. //           setcolors (ilbm,ilbm->vp);
  296.  
  297.            freecolors (ilbm);
  298.         }
  299.      }
  300.  
  301.      closeifile (&(ilbm->ParseInfo));
  302.   }
  303.  
  304.   return (error);
  305. }
  306.  
  307. LONG SavePalette (struct ILBMInfo *ilbm,struct Chunk *chunklist,UBYTE *filename)
  308. {
  309. struct IFFHandle *iff;
  310.  
  311. struct Chunk *chunk;
  312.  
  313. Color32 *colortable32;
  314.  
  315. UWORD ncolors;
  316.  
  317. LONG size,error;
  318.  
  319. ULONG chunkID;
  320.  
  321.   iff = ilbm->ParseInfo.iff;
  322.  
  323.   ncolors = ilbm->vp->ColorMap->Count;
  324.  
  325.   error = openifile (&(ilbm->ParseInfo),filename,IFFF_WRITE);
  326.  
  327.   if (! error)
  328.   {
  329.     error = PushChunk (iff,ID_ILBM,ID_FORM,IFFSIZE_UNKNOWN);
  330.  
  331.     if (colortable32 = (Color32 *) AllocVec ((ULONG) (sizeof (Color32) * ncolors),MEMF_CLEAR))
  332.     {
  333.        GetRGB32 (ilbm->vp->ColorMap,0L,(ULONG) ncolors,(ULONG *) colortable32);
  334.  
  335.        CkErr (putcmap (iff,colortable32,ncolors,32));
  336.  
  337.        FreeVec (colortable32);
  338.     }
  339.  
  340.     for (chunk = chunklist; chunk; chunk = chunk->ch_Next)
  341.     {
  342.       chunkID = chunk->ch_ID;
  343.  
  344.       if ((chunkID != ID_BMHD) && (chunkID != ID_CMAP) && (chunkID != ID_CAMG))
  345.       {
  346.         size = ((chunk->ch_Size == IFFSIZE_UNKNOWN) ? strlen (chunk->ch_Data) : chunk->ch_Size);
  347.  
  348.         CkErr (PutCk (iff,chunkID,size,chunk->ch_Data));
  349.       }
  350.     }
  351.  
  352.     CkErr (PopChunk (iff)); /* close out the FORM */
  353.  
  354.     closeifile (&(ilbm->ParseInfo));    /* and the file */
  355.   }
  356.  
  357.   return (error);
  358. }
  359.  
  360.